/**
 * ForwardStoreServiceImpl.java
 * @author
 * 作者 屈甲康 
 */
package org.jackysoft.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.ForwardStore;
import org.jackysoft.entity.Product;
import org.jackysoft.entity.ProductType;
import org.jackysoft.report.data.ForwardStoreDataImporter;
import org.jackysoft.service.AbstractStringIdServiceImpl;
import org.jackysoft.service.DailyResetDateService;
import org.jackysoft.service.ForwardStoreService;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ForwardStoreServiceImpl extends AbstractStringIdServiceImpl<ForwardStore>
implements ForwardStoreService {
    /**
	 * 
	 */
	private static final long serialVersionUID = 9008154511702262864L;
	final static Log logger = LogFactory.getLog(ForwardStoreServiceImpl.class);
    private ForwardStoreDataImporter dataImporter;
    private DailyResetDateService resetDayService;
	public ForwardStoreServiceImpl(){
	    this.type = ForwardStore.class;
	}

	@Override
	public List<String> importForwardStore(File file, int year,int month) throws FileNotFoundException {
		
		dataImporter.setYear(year);
		dataImporter.setMonth(month);
		clear(year,month);
		Collection<ForwardStore> stores = dataImporter.fromStream(new FileInputStream(file));
		create(stores);
		return null;
		
	}

	@Override
	public ForwardStore get(Department department, int year,int month, Product owner,ProductType pdtType) {
		
		String hql=String.format(
				"from %s f where f.department=:dept and f.owner=:owner and f.itemType=:itemType and f.year=:year and f.month=:month"
				,table());
		Iterator itr = 
				query(hql)
				.setParameter("dept", department)
				.setParameter("owner", owner)
				.setParameter("itemType", pdtType)
				.setInteger("year", year)
				.setInteger("month", month)
				.iterate();
		return (ForwardStore) (itr.hasNext()?itr.next():null);
	}

	@Override
	public Collection<ForwardStore> gets(Department department, int year,int month) {
		String hql=String.format(
				"from %s f where f.department=:dept and year=:year and month=:month"
				,table());
		List itr = 
				query(hql)
				.setParameter("dept", department)
				.setInteger("year", year)
				.setInteger("month", month)
				.list();
		return  itr;
	}

	@Override
	public void clear(Department department) {
		String hql = 
				String.format(
						"delete from %s where department.id=:deptId",table());
		
		query(hql).setString("deptId", department.getId()).executeUpdate();
		
	}

	@Override
	public void clear(int year,int month) {
		String hql = 
				String.format(
						"delete from %s where year=:year and month=:month",table());
		
		query(hql)
		.setInteger("year", year)
		.setInteger("month", month)
		.executeUpdate();
		
		
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void clear(Department department, int year,int month) {
		String hql = 
				String.format(
						"delete from %s where department=:dept and year=:year and month=:month"
						,table()
						);
		
		query(hql)
		.setParameter("dept", department)
		.setInteger("year", year)
		.setInteger("month",month)
		.executeUpdate();
		
	}

	@Resource
	public void setDataImporter(ForwardStoreDataImporter dataImporter) {
		this.dataImporter = dataImporter;
	}

	@Override
	public ForwardStore get(Department department, DateTime date, Product owner,ProductType pdtType) {
		boolean afterResetDay = resetDayService.isEqualOrAfterResetable(date);
		DateTime prevDate = date.withTime(0,0,0,0);
		if(!afterResetDay){
			prevDate  = date.minusMonths(1);
		}
		logger.info(department.getId() + " " + date.getYear() + "-" + date.getMonthOfYear()
				+ "-" + date.getDayOfMonth() + "的结存日期为 " + prevDate.getYear() + "-" + prevDate.getMonthOfYear());
		return get(department, prevDate.getYear(), prevDate.getMonthOfYear(), owner,pdtType);
	}

	@Resource
	public void setResetDayService(DailyResetDateService resetDayService) {
		this.resetDayService = resetDayService;
	}	
		
}